<?php

namespace App\Crontab;

use EasySwoole\EasySwoole\Crontab\AbstractCronTask;
use App\Utility\Pool\MysqlPool;
use EasySwoole\Component\Pool\PoolManager;
use EasySwoole\EasySwoole\Config;
class TaskOne extends AbstractCronTask
{

//    *    *    *    *    *
//    -    -    -    -    -
//    |    |    |    |    |
//    |    |    |    |    |
//    |    |    |    |    +----- day of week (0 - 7) (Sunday=0 or 7)
//    |    |    |    +---------- month (1 - 12)
//    |    |    +--------------- day of month (1 - 31)
//    |    +-------------------- hour (0 - 23)
//    +------------------------- min (0 - 59)
//@yearly(0 0 1 1 *)                     每年一次
//@annually(0 0 1 1 *)                   每年一次
//@monthly(0 0 1 * *)                    每月一次
//@weekly(0 0 * * 0)                     每周一次
//@daily(0 0 * * *)                      每日一次
//@hourly(0 * * * *)                     每小时一次
    public static function getRule(): string
    {
        //定时周期  (每两分钟一次)
        return '*/2 * * * *';
    }

    public static function getTaskName(): string
    {
        //定时任务名称
        return 'taskOne';
    }

    static function run( \swoole_server $server, int $taskId, int $fromWorkerId )
    {
        $db = PoolManager::getInstance()->getPool( MysqlPool::class )->getObj( Config::getInstance()->getConf( 'MYSQL.POOL_TIME_OUT' ) );
        dump('-------------------------');
        dump('开始清理过期会员状态啦');
        $vip=$db->whereIn('level',[2,4])->get('users');
        foreach ($vip as $k=>$v){
            if(strtotime(date('YmdHis'))>=strtotime($v['vip_expire_time'])){
                $db->where('id',$v['id'])->update('users',['level'=>1]);
            }
        }

        dump('清理完成了哦');
        dump('-------------------------');
        dump('开始检测过期活动啦');
        $huodong=$db->where('class_id',46)->where('state',1)->get('study');
        foreach ($huodong as $k=>$v){
            $date=date('Ymd',strtotime($v['date']));
            if(strtotime(date('Ymd')>=strtotime($date))){
                $db->where('id',$v['id'])->update('study',['state'=>0]);
            }
        }
        dump('过期活动检测完成了哦');
        PoolManager::getInstance()->getPool(MysqlPool::class)->recycleObj($db);

    }


}